<template>
    <div class="form">
        <el-form label-width="auto" :rules="formRules" ref="formRef" :model="deviceDateSynchronization">
            <el-form-item label="对象类型" prop="objectType">
                <el-input type="tel" v-model="deviceDateSynchronization.objectType" placeholder="对象类型"></el-input>
            </el-form-item>
            <el-form-item label="设备编码" prop="deviceCode">
                <el-input type="tel" v-model="deviceDateSynchronization.deviceCode" placeholder="请输入"></el-input>
            </el-form-item>
            <el-form-item label="设备id" prop="deviceId">
                <el-input type="tel" v-model="deviceDateSynchronization.deviceId" placeholder="请输入"></el-input>
            </el-form-item>
            <el-form-item label="对象名称" prop="objectName">
                <el-input type="tel" v-model="deviceDateSynchronization.objectName" placeholder="请输入"></el-input>
            </el-form-item>   
            <el-form-item label="同步id" prop="synchronousId">
                <el-input type="tel" v-model="deviceDateSynchronization.synchronousId" placeholder="请填写位置"></el-input>
            </el-form-item>
            <el-form-item label="状态">
                <el-input type="tel" v-model="deviceDateSynchronization.status" placeholder="选填"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="save(formRef)">保存</el-button>
                <el-button type="info" @click="cancel">取消</el-button>
            </el-form-item>
        </el-form>
    </div>

</template>
<script setup>
import http from '../utils/http'
import { ref } from 'vue'
import { ElMessageBox } from 'element-plus'
import { useRouter } from 'vue-router';
import { onBeforeMount } from 'vue';
import DeviceAccessControl from './DeviceDateSynchronization.vue';
let router = useRouter()
const size = ref<'default' | 'large' | 'small'>('default')
const options = [
    {
        value: '业主',
        label: '业主',
    },
    {
        value: '小区',
        label: '小区',
    },
    {
        value: '其他',
        label: '其他',
    }
]
let deviceDateSynchronization = ref({
    id: router.currentRoute.value.query.id,
    objectType: '',
    deviceCode: '',
    deviceId: '',
    objectName: '',
    synchronousId: '',
    status: ''
})
let formRef = ref(null)//表单dom对象
let formRules = {
    deviceCode: [
        {required: true, message: '请输入设备编码', trigger: 'blur' },
    ],
    deviceId: [
        {required: true, message: '请输入设备id', trigger: 'blur' },
],
    objectName: [
        {required: true, message: '请输入对象名称', trigger: 'blur' },
],
    synchronousId: [
        {required: true,message:'请输入对象id',trigger:'blur'},
],
    status: [
        {required: true, message:'请输入状态', trigger:'blur'},
]
}
onBeforeMount(async () => {
    if (deviceDateSynchronization.value.id) {
        let resp = await http.get(`/deviceDateSynchronization/get/${deviceDateSynchronization.value.id}`)
        deviceDateSynchronization.value = resp.data
    }
})
async function save(formRef) {
    formRef.validate(async function (valid) {
         if(valid){
    let path = deviceDateSynchronization.value.id ? '/deviceDateSynchronization/update' : '/deviceDateSynchronization/add'
    let tip = deviceDateSynchronization.value.id ? '修改' : '保存'
    let resp = await http.post(path, deviceDateSynchronization.value)
    if (resp.code == 200) {
        ElMessageBox.alert(tip + '账户成功', tip + '账户', {
            confirmButtonText: 'OK'
        })
        router.go(-1)
    } else {

    }
      }
    })
}
function cancel() {
    router.go(-1)
}

</script>
<style lang="css" scoped>
.form {
    width: 400px;
    margin: 0 auto;
}

.el-form-item__label {
    width: 120px;
}
</style>